﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using gnu.trove.iterator;
using gnu.trove.map.hash;
using java.lang;
using java.lang.reflect;
using lineage2.commons.data.xml;
using Exception = System.Exception;
using String = System.String;

namespace Game.data
{
    public class BoatHolder:AbstractHolder
{
	/**
	 * Field TEMPLATE.
	 */
	public static CharTemplate TEMPLATE = new CharTemplate(CharTemplate.getEmptyStatsSet());
	/**
	 * Field _instance.
	 */
	private static BoatHolder _instance = new BoatHolder();
	/**
	 * Field _boats.
	 */
	private TIntObjectHashMap _boats = new TIntObjectHashMap();
	
	/**
	 * Method getInstance.
	 * @return BoatHolder
	 */
	public static BoatHolder getInstance()
	{
		return _instance;
	}
	
	/**
	 * Method spawnAll.
	 */
	public void spawnAll()
	{
		log();
		for (TIntObjectIterator iterator = _boats.iterator(); iterator.hasNext();)
		{
			iterator.advance();
			iterator.value().spawnMe();
			info("Spawning: " + iterator.value().getName());
		}
	}
	
	/**
	 * Method initBoat.
	 * @param name String
	 * @param clazz String
	 * @return Boat
	 */
	public Boat initBoat(String name, String clazz)
	{
		try
		{
			Class cl = Class.forName("lineage2.gameserver.model.entity.boat." + clazz);
			Constructor constructor = cl.getConstructor(Integer.TYPE, typeof(CharTemplate));
			Boat boat = (Boat) constructor.newInstance(IdFactory.getInstance().getNextId(), TEMPLATE);
			boat.setName(name);
			addBoat(boat);
			return boat;
		}
		catch (Exception e)
		{
			error("Fail to init boat: " + clazz, e);
		}
		return null;
	}
	
	/**
	 * Method initShuttle.
	 * @param name String
	 * @param shuttleId int
	 * @return Shuttle
	 */
	public Shuttle initShuttle(String name, int shuttleId)
	{
		try
		{
			ShuttleTemplate template = ShuttleTemplateHolder.getInstance().getTemplate(shuttleId);
			Shuttle shuttle = new Shuttle(IdFactory.getInstance().getNextId(), template);
			shuttle.setName(name);
			addBoat(shuttle);
			return shuttle;
		}
		catch (Exception e)
		{
			error("Fail to init shuttle id: " + shuttleId, e);
		}
		return null;
	}
	
	/**
	 * Method getBoat.
	 * @param name String
	 * @return Boat
	 */
	public Boat getBoat(String name)
	{
		for (TIntObjectIterator iterator = _boats.iterator(); iterator.hasNext();)
		{
			iterator.advance();
			if (iterator.value().getName().equals(name))
			{
				return iterator.value();
			}
		}
		return null;
	}
	
	/**
	 * Method getBoat.
	 * @param objectId int
	 * @return Boat
	 */
	public Boat getBoat(int objectId)
	{
		return _boats.get(objectId);
	}
	
	/**
	 * Method addBoat.
	 * @param boat Boat
	 */
	public void addBoat(Boat boat)
	{
		_boats.put(boat.getObjectId(), boat);
	}
	
	/**
	 * Method removeBoat.
	 * @param boat Boat
	 */
	public void removeBoat(Boat boat)
	{
		_boats.remove(boat.getObjectId());
	}
	
	/**
	 * Method size.
	 * @return int
	 */
	
	public override int size()
	{
		return _boats.size();
	}
	
	/**
	 * Method clear.
	 */
	
	public override void clear()
	{
		_boats.clear();
	}
}

}
